//
//  XWAlterview.m
//  new
//
//  Created by chinat2t on 14-11-6.
//  Copyright (c) 2014年 chinat2t. All rights reserved.
//

#import "XWAlterview.h"
// 设置警告框的长和宽

#define Alertheigth 100.0f
#define XWtitlegap 15.0f
#define XWtitleofheigth 25.0f
#define XWSinglebuttonWidth 160.0f
//        单个按钮时的宽度
#define XWdoublebuttonWidth 80.0f
//        双个按钮的宽度
#define XWbuttonHeigth 40.0f
//        按钮的高度
#define XWbuttonbttomgap 10.0f
//        设置按钮距离底部的边距


@interface XWAlterview ()<UITextFieldDelegate>
{
    BOOL _leftLeave;
}

@property (nonatomic, strong) UILabel *alertTitleLabel;
@property (nonatomic, strong) UILabel *alertContentLabel;
@property (nonatomic, strong) UIButton *rightbtn;
@property (nonatomic, strong) UIView *backimageView;
@property (nonatomic, strong) UITextField *messageField;
@property (nonatomic, assign) int alertWidth;

@end

@implementation XWAlterview



+ (CGFloat)alertWidth
{
    return self.alertWidth;
}

+ (CGFloat)alertHeight
{
    return Alertheigth;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}
+(XWAlterview*)showmessage:(NSString *)message subtitle:(NSString *)subtitle cancelbutton:(NSString *)cancle
{
    XWAlterview *alert = [[XWAlterview alloc] initWithTitle:message contentText:subtitle leftButtonTitle:nil rightButtonTitle:cancle];
    [alert show];
    alert.rightBlock = ^() {
        NSLog(@"right button clicked");
    };
    alert.dismissBlock = ^() {
        NSLog(@"cancel button clicked");
    };
    return alert;
}


- (id)initWithTitle:(NSString *)title
        contentText:(NSString *)content
    leftButtonTitle:(NSString *)leftTitle
   rightButtonTitle:(NSString *)rigthTitle
{
    if (self = [super init]) {
        self.layer.cornerRadius = 10.0;
        self.backgroundColor = [UIColor colorWithRed:198 green:198 blue:198 alpha:1];
        self.alpha = 0.0;
        
        self.alertWidth = [self getAlterWidth];
        
        self.messageField =[[UITextField alloc]initWithFrame:CGRectMake(0, 2, self.alertWidth, 60)];
        self.messageField.text = content;
        self.messageField.delegate = self;
        self.messageField.backgroundColor = [UIColor clearColor];
        [self.messageField setBorderStyle:UITextBorderStyleNone];
        self.messageField.layer.borderColor=[[UIColor clearColor]CGColor];
        self.messageField.layer.borderWidth= 0.0f;
        self.messageField.textAlignment = NSTextAlignmentCenter;
        [self addSubview:self.messageField];

        self.rightbtn = [UIButton buttonWithType:UIButtonTypeCustom];
        self.rightbtn.frame = CGRectMake(0, Alertheigth - XWbuttonHeigth, self.alertWidth, XWbuttonHeigth);
        self.rightbtn.titleLabel.font = [UIFont fontWithName:@"Hiragino Sans GB" size:15.0];
        [self.rightbtn setTitle:rigthTitle forState:UIControlStateNormal];
        [self.rightbtn setTitleColor:[UIColor colorWithRed:0/255.0 green:122/255.0 blue:255/255.0 alpha:1] forState:UIControlStateNormal];
        [self.rightbtn addTarget:self action:@selector(rightbtnclicked:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.rightbtn setBackgroundImage:[UIImage imageNamed:@"bg_mr_midi_detail"] forState:UIControlStateHighlighted];
        
        [self addSubview:self.rightbtn];
        
        UIView *line = [[UIView alloc] init];
        line.frame = CGRectMake(0, self.rightbtn.frame.origin.y - 1, self.alertWidth, 1);
        line.backgroundColor = [UIColor colorWithRed:183 / 255.0 green:183 / 255.0 blue:183 / 255.0 alpha:0.5];
        [self addSubview:line];

        self.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin;
    }
    return self;
}

- (int)getAlterWidth
{
    if ([UIScreen mainScreen].applicationFrame.size.width==320)
    {
        if([UIScreen mainScreen].applicationFrame.size.height == 460)
        {
            return 220;
        }
        else
        {
            return 220;
        }
    }else if ([UIScreen mainScreen].applicationFrame.size.width==375)
    {
        return 300;
        
    }else if ([UIScreen mainScreen].applicationFrame.size.width==414)
    {
        return 300;
    }
    else
    {
        return 300;
    }
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    [textField resignFirstResponder]; //这句代码可以隐藏 键盘
    return NO;
}

- (void)leftbtnclicked:(id)sender
{
    if (self.leftBlock) {
        self.leftBlock();
    }
    [self dismissAlert];
}

- (void)rightbtnclicked:(id)sender
{
    if (self.rightBlock) {
        self.rightBlock();
    }
    [self dismissAlert];
}
- (void)show
{   //获取第一响应视图视图
    UIViewController *topVC = [self appRootViewController];
    self.frame = CGRectMake((CGRectGetWidth(topVC.view.bounds) - self.alertWidth) * 0.5-30, (CGRectGetHeight(topVC.view.bounds) - Alertheigth) * 0.5-20, self.alertWidth, Alertheigth);
    [topVC.view addSubview:self];
}

- (void)dismissAlert
{
    [self removeFromSuperview];
    if (self.dismissBlock) {
        self.dismissBlock();
    }
}

- (UIViewController *)appRootViewController
{

    UIViewController *appRootVC = [UIApplication sharedApplication].keyWindow.rootViewController;
    UIViewController *topVC = appRootVC;
    while (topVC.presentedViewController) {
        topVC = topVC.presentedViewController;
    }
    return topVC;
}


- (void)removeFromSuperview
{
//    UIViewController *topVC = [self appRootViewController];
    
//    CGRect afterFrame = CGRectMake((CGRectGetWidth(topVC.view.bounds) - self.alertWidth) * 0.5+30, (CGRectGetHeight(topVC.view.bounds) - Alertheigth) * 0.5-30, self.alertWidth, Alertheigth);
    
    [UIView animateWithDuration:0.2f delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{

        self.alpha = 0.0;
        self.backimageView.alpha = 0.0;
        
    } completion:^(BOOL finished) {
        
        [self.backimageView removeFromSuperview];
        self.backimageView = nil;
        
        [super removeFromSuperview];
    }];
}

//添加新视图时调用（在一个子视图将要被添加到另一个视图的时候发送此消息）
- (void)willMoveToSuperview:(UIView *)newSuperview
{
    if (newSuperview == nil) {
        return;
    }
    //     获取根控制器
    UIViewController *topVC = [self appRootViewController];
    
    if (!self.backimageView) {
        self.backimageView = [[UIView alloc] initWithFrame:topVC.view.bounds];
        self.backimageView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1];
        self.backimageView.alpha = 0.3f;
        self.backimageView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    }
    //    加载背景图,防止重复点击
    [topVC.view addSubview:self.backimageView];
    
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
    
    [self.backimageView addGestureRecognizer:tapGestureRecognizer];
    
    CGRect afterFrame = CGRectMake((CGRectGetWidth(topVC.view.bounds) - self.alertWidth) * 0.5, (CGRectGetHeight(topVC.view.bounds) - Alertheigth) * 0.5, self.alertWidth, Alertheigth);
    
    self.frame = afterFrame;
    
    [UIView animateWithDuration:0.2f delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
        
        self.alpha = 1.0;
        
    } completion:^(BOOL finished) {
    }];
    
    [super willMoveToSuperview:newSuperview];
}

-(void)tap
{
    [self dismissAlert];
}


@end















